#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <QPushButton>
#include "AuthenticationMain.h"
#include "DeviceMain.h"
#include "MonitorData.h"
#include "Tools/MqttClient.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MqttClient::getInstance();
    this->resize(1400, 800);
    this->InitUi();

}

void MainWindow::handlButton()
{
    QList<QPushButton*> button_list = {
        this->ui->pushButton_device,
        this->ui->pushButton_authentication,
        this->ui->pushButton_data
    };


}

void MainWindow::InitUi()
{
    QList<QPushButton*> button_list = {
        this->ui->pushButton_device,
        this->ui->pushButton_authentication,
        this->ui->pushButton_data
    };

    QList<QWidget*> page_list = {
        new AuthenticationMain(),
        new DeviceMain(),
        new MonitorData()
    };

    QList<QString> noChecked_image = {
        "E:/ComplexProject/IOT_DM/IOT_DM_Client/IOT_DM/Resources/authCheck.svg",
        "E:/ComplexProject/IOT_DM/IOT_DM_Client/IOT_DM/Resources/deviceCheck.svg",
        "E:/ComplexProject/IOT_DM/IOT_DM_Client/IOT_DM/Resources/mointorCheck.svg",
    };

    QList<QString> Checked_image = {
        "E:/ComplexProject/IOT_DM/IOT_DM_Client/IOT_DM/Resources/authChecked.svg",
        "E:/ComplexProject/IOT_DM/IOT_DM_Client/IOT_DM/Resources/deviceChecked.svg",
        "E:/ComplexProject/IOT_DM/IOT_DM_Client/IOT_DM/Resources/mointorChecked.svg",
    };

    int index = 0;
    foreach(QPushButton* button, button_list)
    {
        this->ui->stackedWidget->insertWidget(index, page_list[index]);

        connect(button, &QPushButton::clicked, this, [=](){
            QPushButton* buttonSender = qobject_cast<QPushButton*>(sender());
            if(buttonSender)
            {
                int index = 0;
                foreach(QPushButton* btn, button_list)
                {
                    if(btn == buttonSender)
                    {
                        buttonSender->setChecked(true);
                        buttonSender->setIcon(QIcon(Checked_image[index]));
                    }
                    else
                    {
                        btn->setChecked(false);
                        btn->setIcon(QIcon(noChecked_image[index]));
                    }
                    index++;
                }

            }
            // 这里是捕获的index
            this->ui->stackedWidget->setCurrentIndex(index);
        });
        index++;
    }
    this->ui->stackedWidget->setCurrentIndex(0);

    this->ui->pushButton_device->setChecked(true);
    this->ui->pushButton_device->setIcon(QIcon(Checked_image[0]));
}

MainWindow::~MainWindow()
{
    delete ui;
}
